//
//  SortViewController.m
//  Menu
//
//  Created by Lynn-Shin on 15/7/6.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "SortViewController.h"

@interface SortViewController ()

@end

@implementation SortViewController

-(void)dealloc{
    
    [_sortTableV release];
    [_sortCollectV release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"分类";
    self.view.backgroundColor = [UIColor whiteColor];
    self.sortTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, MyWidth, MyHeight-64-49) style:UITableViewStylePlain];
    [self.view addSubview:self.sortTableV];
    [self.sortTableV release];
    
    self.sortTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100*WIDTH, 40*HEIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(10*HEIGHT, 10*WIDTH, 10*HEIGHT, 10*WIDTH);
    
    self.sortCollectV = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.sortCollectV.backgroundColor = [UIColor lightGrayColor];
    //[self.view addSubview:self.sortCollectV];
    [self.sortCollectV release];
    
    self.sortTableV.delegate = self;
    self.sortTableV.dataSource =  self;
    
    
    [self.sortCollectV registerClass:[SortCollectionViewCell class] forCellWithReuseIdentifier:@"Reuse"];
    self.sortCollectV.delegate = self;
    self.sortCollectV.dataSource = self;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    
    self.isOpen = YES;
    self.allSortArray = [NSMutableArray array];
    self.countArray = [NSMutableArray array];
    self.sectionNum = 0;
    [self dowmLoadData];
    
    // Do any additional setup after loading the view.
}

-(void)dowmLoadData{
    
    [AFNTool get:@"http://121.41.84.91:80/HandheldKitchen/api/home/tblAssort!getFirstgrade.do" Body:nil HttpResult:JSON success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            
            SortModel *sortModel = [[SortModel alloc] initWithDic:dic];
            [self.allSortArray addObject:sortModel];
            [self.countArray addObject:sortModel.secondgrade];
        }
        [self.allSortArray removeLastObject];
        [self.allSortArray removeLastObject];
        [self.allSortArray removeLastObject];
        [self.allSortArray removeObjectAtIndex:19];
        [self.countArray removeLastObject];
        [self.countArray removeLastObject];
        [self.countArray removeLastObject];
        [self.countArray removeObjectAtIndex:19];
        [self.sortTableV reloadData];
        self.netOK = YES;
        [self.HUD hide:YES];
    } failure:^(NSError *error) {
        
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:1];
    }];
    
}

//********************tableView***********************

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.allSortArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (self.isOpen == NO) {
        
        if (self.sectionNum == section && self.isOpen == NO) {
            
            NSMutableArray *arr = [self.countArray objectAtIndex:section];
            self.sortCollectV.alpha = 0;
             [UIView animateWithDuration:1.0 animations:^{ [self.sortCollectV setAlpha:1.0f];
             }];
            return ((arr.count+2)/3*51+10)*HEIGHT;
            
        } else {
            
            return 0;
        }
    }
    return 0;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
//    static NSString *footIndentifier = @"myFoot";
//    UITableViewHeaderFooterView *footView = [tableView dequeueReusableCellWithIdentifier:footIndentifier];
//    if (footView == nil) {
//        footView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:footIndentifier];
//    }
//    [footView addSubview:self.sortCollectV];
    
    return self.sortCollectV;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (self.isClick == NO) {
        
        self.isOpen = NO;
        
        self.sectionNum = indexPath.section;
        [self.sortTableV reloadData];
        self.isClick = YES;
    } else {
        
        self.isOpen = YES;
        
        self.sectionNum = indexPath.section;
        [self.sortTableV reloadData];
        self.isClick = NO;
    }
    if (self.isOpen == NO) {
        
        NSIndexPath *indexPaths = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [self.sortTableV scrollToRowAtIndexPath:indexPaths atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
//    NSLog(@"%ld %ld",indexPath.section, self.sectionNum);
    self.sectionNum = indexPath.section;
    [self.sortCollectV reloadData];
    [self.sortTableV reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"myCell";
    SortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        
        cell = [[SortTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if (indexPath.section == self.sectionNum && self.isClick) {
        
        [UIView animateWithDuration:0.5 animations:^{
            cell.arrowImage.transform = CGAffineTransformMakeRotation(-M_PI);
        } completion:^(BOOL finished) {
            
        }];
    } else {
        
        [UIView animateWithDuration:0.5 animations:^{
            cell.arrowImage.transform = CGAffineTransformMakeRotation(M_PI*2);
        } completion:^(BOOL finished) {
            
        }];
    }
    cell.sortModel = [self.allSortArray objectAtIndex:indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60*HEIGHT;
}

//********************collectionView***********************

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Reuse" forIndexPath:indexPath];

    cell.nameLabel.text = [[[self.countArray objectAtIndex:self.sectionNum] objectAtIndex:indexPath.item] objectForKey:@"name"];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!self.netOK) {
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"请求失败";
        [self.HUD show:YES];
        [self.HUD hide:YES afterDelay:0.5];
        return;
    }
    GoodListViewController *goodListVC = [[GoodListViewController alloc] init];
    goodListVC.typeId = [[[[self.countArray objectAtIndex:self.sectionNum] objectAtIndex:indexPath.item] objectForKey:@"id"] integerValue];
    goodListVC.navigationItem.title = [[[self.countArray objectAtIndex:self.sectionNum] objectAtIndex:indexPath.item] objectForKey:@"name"];
    goodListVC.isSort = YES;
    [self.navigationController pushViewController:goodListVC animated:YES];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSMutableArray *arr = [self.countArray objectAtIndex:self.sectionNum];
//    NSLog(@"********%ld*********%ld",arr.count, section);
    return arr.count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
